package com.itheima.search.controller;

import com.itheima.search.pojo.Question;
import com.itheima.search.service.EsSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <dl>
 * <dd>描述: ~节点业务逻辑实现</dd>
 * <dd>创建时间：  15:29 2020/6/23</dd>
 * <dd>创建人： zz</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2020/6/23      guodong       1.0        1.0 Version
 * </pre>
 * </dl>
 */
@Api("搜索controller")
@Controller
@RequestMapping("search")
public class SearchController {

    @Autowired
    private EsSearchService searchService;

    @ApiOperation("跳转值搜索页面")
    @GetMapping
    public String toSearchPage(){
        return "search";
    }

    @ApiOperation("根据问题搜索答案")
    @ApiImplicitParam(name = "title", value = "问题")
    @PostMapping("/answer")
    public String answer(String title, Model model){
        Map<String, Object> resultMap = searchService.search(title);
        model.addAttribute("questionList", resultMap.get("questionList"));
        return "answer";
    }
    /**
    * @Date: 15:33 2020/6/23
    * @Param: [question]
    * @return: java.lang.String
    * @Description: 添加问题
    **/
    @ApiOperation("新增问题")
    @PostMapping("addQuestion")
    @ResponseBody
    public String addQuestion(@RequestBody Question question){
        try {
            searchService.addQuestion(question);
            return "添加问题成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "添加问题失败";
        }
    }
}
